package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    ApartmentLabelService apartmentLabelService;

    @Autowired
    ApartmentFacilityService apartmentFacilityService;

    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    ProvinceInfoMapper provinceInfoMapper;

    @Autowired
    CityInfoMapper cityInfoMapper;

    @Autowired
    DistrictInfoMapper districtInfoMapper;

    @Autowired
    LabelInfoMapper labelInfoMapper;

    @Autowired
    FacilityInfoMapper facilityInfoMapper;

    @Autowired
    FeeValueMapper feeValueMapper;


    @Autowired
    RoomInfoMapper roomInfoMapper;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        Long apartmentId = apartmentSubmitVo.getId();

        if (apartmentId != null){
            // 1 删除图片
            QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
            graphInfoQueryWrapper.eq("item_type",1);  //??
            graphInfoQueryWrapper.eq("item_id",apartmentId);
            graphInfoService.remove(graphInfoQueryWrapper);
            // 2 删除配套
            QueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper  = new QueryWrapper<>();
            apartmentFacilityQueryWrapper.eq("apartment_id",apartmentId);
            apartmentFacilityService.remove(apartmentFacilityQueryWrapper);
            // 3 删除标签
            QueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new QueryWrapper<>();
            apartmentLabelQueryWrapper.eq("apartment_id",apartmentId);
            apartmentLabelService.remove(apartmentLabelQueryWrapper);

            // 4 删除杂费
            QueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new QueryWrapper<>();
            apartmentFeeValueQueryWrapper.eq("apartment_id",apartmentId);
            apartmentFeeValueService.remove(apartmentFeeValueQueryWrapper);
        }

        //新增或更新公寓
        Long provinceId = apartmentSubmitVo.getProvinceId();
        Long cityId = apartmentSubmitVo.getCityId();
        Long districtId = apartmentSubmitVo.getDistrictId();
        ProvinceInfo provinceInfo = provinceInfoMapper.selectById(provinceId);
        CityInfo cityInfo = cityInfoMapper.selectById(cityId);
        DistrictInfo districtInfo = districtInfoMapper.selectById(districtId);
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());
        apartmentSubmitVo.setCityName(cityInfo.getName());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());
        this.saveOrUpdate(apartmentSubmitVo); //这里问什么要用this???
        apartmentId = apartmentSubmitVo.getId();

        // 1 插入图片
//        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
//        List<GraphInfo> graphInfos  = graphVoList.stream().map(graphVo -> {
//            GraphInfo graphInfo = new GraphInfo();    //???
//            graphInfo.setUrl(graphInfo.getUrl());
//            graphInfo.setName(graphInfo.getName());
//            graphInfo.setItemType(graphInfo.getItemType());
//            graphInfo.setItemId(graphInfo.getId());
//            return graphInfo;
//        }).collect(Collectors.toList());
//        graphInfoService.saveBatch(graphInfos);
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfos =  graphVoList.stream().map(graphVo->{
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setName(graphVo.getName());
            graphInfo.setItemType(ItemType.APARTMENT);
                                      //??
            graphInfo.setItemId(apartmentSubmitVo.getId());
            return graphInfo;
        }).collect(Collectors.toList());
        graphInfoService.saveBatch(graphInfos);
        // 2 插入配套
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
        for (Long facilityInfoId : facilityInfoIds) {
            //被@Builder标记了，只能通过建造者设计模式创建了
            ApartmentFacility apartmentFacility = ApartmentFacility.builder().build();
            apartmentFacility.setApartmentId(apartmentId);
            apartmentFacility.setFacilityId(facilityInfoId);
            apartmentFacilities.add(apartmentFacility);
        }
        apartmentFacilityService.saveBatch(apartmentFacilities);

        // 3 插入标签
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();   //这个是什么意思？？
        ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
        for (Long labelId : labelIds) {
            ApartmentLabel apartmentLabel = ApartmentLabel.builder().build();
            apartmentLabel.setApartmentId(apartmentId);
            apartmentLabel.setLabelId(labelId);
            apartmentLabels.add(apartmentLabel);
        }
        apartmentLabelService.saveBatch(apartmentLabels);

        // 4 插入杂费
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        for (Long feeValueId : feeValueIds) {
            ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().build();
            apartmentFeeValue.setApartmentId(apartmentId);
            apartmentFeeValue.setFeeValueId(feeValueId);
            apartmentFeeValues.add(apartmentFeeValue);
        }
        apartmentFeeValueService.saveBatch(apartmentFeeValues);

    }

    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentInfo> apartmentInfoIPage, ApartmentQueryVo queryVo) {
        IPage<ApartmentItemVo> itemVoIPage = baseMapper.selectApartmentItemVoPage(apartmentInfoIPage,queryVo);

        return itemVoIPage;
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        //这个公寓的详细信息为什么要new出来？
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //
        ApartmentInfo apartmentInfo = baseMapper.selectById(id);
        //ApartmentDetailVo和ApartmentInfo是继承关系，这里完成属性的复制，为什么要复制呢，不是已经继承了么
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);

        //图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        List<GraphInfo> graphInfos = graphInfoService.list(graphInfoLambdaQueryWrapper);
        List<GraphVo> graphVoList = graphInfos.stream().map(graphInfo ->{
                    GraphVo graphVo = new GraphVo();
                    BeanUtils.copyProperties(graphInfo,graphVo);
                    return graphVo;
                }

        ).collect(Collectors.toList());
        apartmentDetailVo.setGraphVoList(graphVoList);

        //标签信息
        //List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartment();
        //apartmentDetailVo.setLabelInfoList(labelInfoList);

        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);
        apartmentDetailVo.setLabelInfoList(labelInfoList);

        //配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);

        //杂费信息
        //为什么是FeeValueVo？？  Mapper中查询的数据为什么就自动封装到FeeValueVo上了？
        List<FeeValueVo> feeValueVos = feeValueMapper.selectListByApartmentId(id);
        apartmentDetailVo.setFeeValueVoList(feeValueVos);
        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long apartmentId) {
        // 1 删除图片
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type",1);
        graphInfoQueryWrapper.eq("item_id",apartmentId);
        graphInfoService.remove(graphInfoQueryWrapper);

        // 2 删除配套
        QueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new QueryWrapper<>();
        apartmentFacilityQueryWrapper.eq("apartment_id",apartmentId);
        apartmentFacilityService.remove(apartmentFacilityQueryWrapper);

        // 3 删除标签
        QueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new QueryWrapper<>();
        apartmentLabelQueryWrapper.eq("apartment_id",apartmentId);
        apartmentLabelService.remove(apartmentLabelQueryWrapper);

        // 4 删除杂费
        QueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new QueryWrapper<>();
        apartmentFeeValueQueryWrapper.eq("apartment_id",apartmentId);
        apartmentFeeValueService.remove(apartmentFeeValueQueryWrapper);

        // 5 删除公寓信息
        QueryWrapper<RoomInfo> roomInfoQueryWrapper = new QueryWrapper<>();
        roomInfoQueryWrapper.eq("apartment_id",apartmentId);
        roomInfoMapper.delete(roomInfoQueryWrapper);

        //对这里的this有点疑问？？？
        this.removeById(apartmentId);

    }

}




